package com.kye.express.business.splash;

import android.databinding.DataBindingUtil;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.base.Logger;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.business.main.MainActivity;
import com.kye.express.databinding.ActivitySplashBinding;
import com.kye.express.utils.ActivityManager;

import java.util.concurrent.TimeUnit;

import io.reactivex.Single;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class SplashActivity extends BaseAppCompatActivity implements SplashContract.SplashView {

    private ActivitySplashBinding mBinding;
    private SplashContract.SplashPresenter mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_splash);

        if (ActivityManager.getInstance().getCurrentActivity() != null) {
            startToMainActivity();
        }

        initEvent();
        initData();
    }

    private void startToMainActivity() {
        finish();
        MainActivity.startActivity(SplashActivity.this);
    }

    private void initEvent() {
        Disposable subscribeSkip = null;
        Disposable subscribeTimer = null;
        subscribeTimer = Single.timer(3, TimeUnit.SECONDS)
                .doOnDispose(new Action() {
                    @Override
                    public void run() throws Exception {
                        Logger.d("doOnDispose on timer");
                    }
                })
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        startToMainActivity();
                    }
                });
        Disposable finalSubscribeTimer = subscribeTimer;
        subscribeSkip = RxView.clicks(mBinding.buttonSkip)
                .compose(new DebounceObservableTransformer<>())
                .compose(this.bindToLifecycleDestroy())
                .take(1)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        finalSubscribeTimer.dispose();
                        startToMainActivity();
                    }
                });
        mPresenter = new SplashContract.SplashPresenter();
        mPresenter.addDisposable(subscribeSkip);
        mPresenter.addDisposable(subscribeTimer);
    }

    private void initData() {
        mPresenter.bind(this);
        mPresenter.getSplashAd();
        mPresenter.getAndSaveAdInfo();
    }

    @Override
    public void onADSplashImageUrl(String adImageUrl) {
        Glide.with(this)
                .load(adImageUrl)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        mBinding.setSkipVisibility(true);
                        return false;
                    }
                })
                .into(mBinding.imageViewSplash);
    }
}
